<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CZLJchat</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f0f0f0;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
        }
        .chat-box {
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            padding: 20px;
            margin-bottom: 20px;
        }
        #messageList {
            height: 400px;
            overflow-y: auto;
            margin-bottom: 10px;
        }
        .message {
            margin: 10px 0;
            padding: 10px;
            border-radius: 5px;
            max-width: 70%;
        }
        .user-message {
            background-color: #e3f2fd;
            float: right;
        }
        .assistant-message {
            background-color: #f5f5f5;
            clear: both;
        }
        input[type="text"] {
            width: 70%;
            padding: 10px;
            font-size: 16px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        button {
            width: 20%;
            padding: 10px;
            background-color: #2196F3;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        button:hover {
            background-color: #1976D2;
        }
        .loading {
            display: none;
            color: #666;
            font-style: italic;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>CZLJchat</h1>
        <div class="chat-box">
            <div id="messageList"></div>
            <div class="loading" id="isLoading">正在等待回复...</div>
            <div style="display: flex; gap: 10px;">
                <input type="text" id="userInput" placeholder="请输入问题...">
                <button onclick="sendMessage()">发送</button>
            </div>
        </div>
    </div>

    <script>
        const APPID = '';
        const API_KEY = '';
        const API_SECRET = '';
        let ws;

        function initWebSocket() {
            const wsUrl = 'wss://spark-api.xf-yun.com/v4.0/chat';
            ws = new WebSocket(wsUrl);
            
            ws.onopen = function(event) {
                const headers = {
                    'appid': APPID,
                    'apikey': API_KEY,
                    'apisecret': API_SECRET
                };
                event.target.headers = headers;
                ws.send(JSON.stringify({ "role": "user", "content": "" }));
            };

            ws.onmessage = function(event) {
                const data = JSON.parse(event.data);
                if (data.type === 'msg') {
                    displayAssistantMessage(data.content);
                } else if (data.type === 'heartbeat') {
                    ws.send(JSON.stringify({ "type": "heartbeat_response" }));
                }
            };

            ws.onclose = function(event) {
                console.log('WebSocket连接已关闭');
                document.getElementById('isLoading').style.display = 'none';
            };
        }

        function sendMessage() {
            const userInput = document.getElementById('userInput');
            const message = userInput.value.trim();
            if (message === '') return;
            displayUserMessage(message);
            ws.send(JSON.stringify({ "role": "user", "content": message }));
            userInput.value = '';
        }

        function displayUserMessage(content) {
            const messageDiv = document.createElement('div');
            messageDiv.className = 'message user-message';
            messageDiv.textContent = content;
            document.getElementById('messageList').appendChild(messageDiv);
        }

        function displayAssistantMessage(content) {
            const messageDiv = document.createElement('div');
            messageDiv.className = 'message assistant-message';
            messageDiv.textContent = content;
            document.getElementById('messageList').appendChild(messageDiv);
        }

        document.getElementById('userInput').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                sendMessage();
            }
        });

        initWebSocket();
    </script>
</body>
</html>